﻿using GrainManage.Data.Pattern;
using GrainManage.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GrainManage.Web.Controllers
{
    public class BaseController : Controller
    {
        protected readonly IPattern Filter = ObjectFactory.Filter;

        protected AppInfo CurrentAccount
        {
            get
            {
                return new AppInfo { Guid = "2a56329c322f11e2b874902b344b9bb0", LoginIP = GrainManage.Web.Common.RequestHelper.GetIP(), UserName = "bijinshu" };
                //return Session["CurrentAccount"] as AppInfo;
            }
            set
            {
                Session["CurrentAccount"] = value;
            }
        }

        protected static NewtonsoftJsonResult NewtonsoftJson(object data)
        {
            return new NewtonsoftJsonResult { Data = data };
        }

        protected string GetSession(string key)
        {
            return Session[key] as string;
        }

        protected T GetSession<T>(string key)
        {
            T result = default(T);
            try
            {
                var obj = Session[key];
                if (obj != null)
                {
                    Type modelType = typeof(T);
                    if (modelType.IsValueType)
                    {
                        result = (T)Convert.ChangeType(obj, modelType);
                    }
                    else
                    {
                        result = (T)obj;
                    }
                }
            }
            catch (Exception)
            {
            }
            return result;
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            log4net.ILog log = log4net.LogManager.GetLogger("LogToSqlite");
            var e = filterContext.Exception;
            log.Error(e.Message, e);
        }
    }
}
